package hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import java.io.IOException;

/**
 * @describe: 一自定义mr将Hadoop hdfs中的数据导入到 Hbase
 *
 * 步骤：
 *  1.开发MR 并打Jar包
 *  2.Hbase创建表 create 'stu','info1'
 *  3.运行MR
 *      hadoop jar ReadHdfsToHbase.jar hadoop.ReadHdfsToHbase  /stu.txt  stu
 *  4.Hbase 查看数据
 *      scan ‘stu’
 *
 * 数据：id name age gender birthday
 *  1    zhangsan    10    male    NULL
 *  2    lisi    NULL    NULL    NULL
 *  3    wangwu    NULL    NULL    NULL
 *  4    zhaoliu    NULL    NULL    1993
 */
public class ReadHdfsToHbase {
    //列族
    public static final String CF = "info1";

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop1:2181");
        conf.set("hbase.rootdir", "hdfs://hadoop1:9000/HBase");
        conf.set(TableOutputFormat.OUTPUT_TABLE, args[1]);
        Job job = Job.getInstance(conf, ReadHdfsToHbase.class.getSimpleName());
        TableMapReduceUtil.addDependencyJars(job);
        job.setJarByClass(ReadHdfsToHbase.class);

        job.setMapperClass(ReadHdfsToHbaseMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(ReadHdfsToHbaseReducer.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        job.setOutputFormatClass(TableOutputFormat.class);
        job.waitForCompletion(true);
    }

    public static class ReadHdfsToHbaseMapper extends Mapper<LongWritable, Text, Text, Text> {
        private final Text outKey = new Text();
        private final Text outValue = new Text();

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] splits = value.toString().split("\t");
            outKey.set(splits[0]);
            outValue.set(splits[1]+"\t"+splits[2]+"\t"+splits[3]+"\t"+splits[4]);
            context.write(outKey, outValue);
        }
    }

    public static class ReadHdfsToHbaseReducer extends TableReducer<Text, Text, NullWritable> {

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            super.reduce(key, values, context);
            Put put = new Put(key.getBytes());
            for (Text text : values) {
                String[] splis = text.toString().split("\t");
                if (splis[0] != null && !"NULL".equals(splis[0])) {
                    put.addColumn(CF.getBytes(), "name".getBytes(), splis[0].getBytes());
                }
                if (splis[1] != null && !"NULL".equals(splis[1])) {
                    put.addColumn(CF.getBytes(), "age".getBytes(), splis[1].getBytes());
                }
                if (splis[2] != null && !"NULL".equals(splis[2])) {
                    put.addColumn(CF.getBytes(), "gender".getBytes(), splis[2].getBytes());
                }
                if (splis[3] != null && !"NULL".equals(splis[3])) {
                    put.addColumn(CF.getBytes(), "birthday".getBytes(), splis[3].getBytes());
                }
            }
            context.write(NullWritable.get(), put);
        }
    }
}
